<?php
/**
 * WooCommerce Shipping Settings
 *
 * @author      WooThemes
 * @category    Admin
 * @package     WooCommerce/Admin
 * @version     2.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( ! class_exists( 'WC_HelloWorld_Settings' ) ) :

/**
 * WC_HelloWorld_Settings
 */
class WC_HelloWorld_Settings extends WC_Settings_Page {

	/**
	 * Constructor.
	 */
	public function __construct() {

		$this->id    = 'wc-helloworld';
		$this->label = __( 'Helllo World', 'Settings tab label', 'woocommerce' );  // old,?? _x(...)

		add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
		add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
		add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
		// add_action( 'woocommerce_admin_field_payment_gateways', array( $this, 'payment_gateways_setting' ) );
		add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
	}

	/**
	 * Get sections
	 *
	 * @return array
	 */
	public function get_sections() {

	
		$sections = array(
			'shop_loop123'    => __( 'Shop', 'wc-helloworld' ),
			'product_pageAAA' => __( 'Product', 'wc-helloworld' ),
		);
		
		// $sections = array(
			// ''         => __( 'Checkout Options', 'woocommerce' )
		// );

		// // Load shipping methods so we can show any global options they may have
		// $payment_gateways = WC()->payment_gateways->payment_gateways();

		// foreach ( $payment_gateways as $gateway ) {

			// $title = empty( $gateway->method_title ) ? ucfirst( $gateway->id ) : $gateway->method_title;

			// $sections[ strtolower( get_class( $gateway ) ) ] = esc_html( $title );
		// }

		return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
	}

	/**
	 * Get settings array
	 *
	 * @return array
	 */
	public function get_settings() {
		// 
		
		// $setting =	array(
		$settings = apply_filters( 'woocommerce_payment_gateways123_settings', 
			array(
			'shop_loop123' =>
				array(

					array(	'title' => __( 'Checkout Process', 'woocommerce' ), 'type' => 'title', 'id' => 'checkout_process_options123' ),

					// 'default'  => 'yes',
					array(
						'title'    => __( 'Coupons', 'woocommerce' ),
						'desc'     => __( 'Enable the use of coupons', 'woocommerce' ),
						'id'       => 'woocommerce_enable_coupons123',
						
						'type'     => 'checkbox',
						'desc_tip' =>  __( 'Coupons can be applied from the cart and checkout pages.', 'woocommerce' ),
						'autoload' => false
					),

				)
			)
		);
		
		return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings );
		
		
		// $current_section = isset( $GLOBALS['current_section'] ) ? $GLOBALS['current_section'] : 'shop_loop';
		// return isset( $settings[ $current_section ] ) ?  $settings[ $current_section ] : $settings['shop_loop'];
	}

	
	
	/**
	 * Return the customization value for the given filter
	 *
	 * @since 2.0.0
	 * @return string
	 */
	public function get_customization() {

		$filterAAA = str_replace( 'pre_option_', '', current_filter() ); // ??
		return isset( $this->customizations[ $filterAAA ] ) ? $this->customizations[ $filterAAA ] : '';
		// return '1';
	}
	/**
	 * Output the settings
	 */
	public function output() {
		global $current_section;

		
		// $settings = $this->get_settings();

		// // inject the actual setting value before outputting the fields
		// // ::output_fields() uses get_option() but customizations are stored
		// // in a single option so this dynamically returns the correct value
		// foreach ( $this->customizations as $filter => $value ) {

			// add_filter( "pre_option_{$filter}", array( $this, 'get_customization' ) ); // ??
		// }

		// WC_Admin_Settings::output_fields( $settings );
		
		
		
		
		
		// Load shipping methods so we can show any global options they may have
		
			$settings = $this->get_settings();
			WC_Admin_Settings::output_fields( $settings );
			
			
		// $payment_gateways = WC()->payment_gateways->payment_gateways();

		// if ( $current_section ) {

 			// foreach ( $payment_gateways as $gateway ) {
				// ?? get_class
				// ok, ?? current_section: wc_gateway_bacs, wc_gateway_cheque...
				// if ( strtolower( get_class( $gateway ) ) == strtolower( $current_section ) ) {  
					// $gateway->admin_options();
					// break;
				// }
			// }
 		// } else {
			// $settings = $this->get_settings();
			// WC_Admin_Settings::output_fields( $settings );
		// }
	}

	/**
	 * Output payment gateway settings.
	 */
	public function payment_gateways_setting() {
		?>
		<tr valign="top">
			<th scope="row" class="titledesc"><?php _e( 'Gateway Display Order', 'woocommerce' ) ?></th>
		    <td class="forminp">
				<table class="wc_gateways widefat" cellspacing="0">
					<thead>
						<tr>
							<?php
								$columns = apply_filters( 'woocommerce_payment_gateways_setting_columns', array(
									'default'  => __( 'Default', 'woocommerce' ),
									'name'     => __( 'Gateway', 'woocommerce' ),
									'id'       => __( 'Gateway ID', 'woocommerce' ),
									'status'   => __( 'Status', 'woocommerce' ),
									'settings' => ''
								) );

								foreach ( $columns as $key => $column ) {
									echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
								}
							?>
						</tr>
					</thead>
					<tbody>
						<?php
						$default_gateway = get_option( 'woocommerce_default_gateway' );

						foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {

							echo '<tr>';

							foreach ( $columns as $key => $column ) {

								switch ( $key ) {

									case 'default' :
										echo '<td width="1%" class="default">
											<input type="radio" name="default_gateway" value="' . esc_attr( $gateway->id ) . '" ' . checked( $default_gateway, esc_attr( $gateway->id ), false ) . ' />
											<input type="hidden" name="gateway_order[]" value="' . esc_attr( $gateway->id ) . '" />
										</td>';
									break;

									case 'name' :
										echo '<td class="name">
											' . $gateway->get_title() . '
										</td>';
									break;

									case 'id' :
										echo '<td class="id">
											' . esc_html( $gateway->id ) . '
										</td>';
									break;

									case 'status' :
										echo '<td class="status">';

										if ( $gateway->enabled == 'yes' )
											echo '<span class="status-enabled tips" data-tip="' . __ ( 'Enabled', 'woocommerce' ) . '">' . __ ( 'Enabled', 'woocommerce' ) . '</span>';
										else
											echo '-';

										echo '</td>';
									break;

									case 'settings' :
										echo '<td class="settings">
											<a class="button" href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( get_class( $gateway ) ) ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>
										</td>';
									break;

									default :
										do_action( 'woocommerce_payment_gateways_setting_column_' . $key, $gateway );
									break;
								}
							}

							echo '</tr>';
						}
						?>
					</tbody>
				</table>
			</td>
		</tr>
		<?php
	}

	/**
	 * Save settings
	 */
	public function save() {
		global $current_section;

		if ( ! $current_section ) {

			$settings = $this->get_settings();

			WC_Admin_Settings::save_fields( $settings );
			WC()->payment_gateways->process_admin_options();

		} elseif ( class_exists( $current_section ) ) {

			$current_section_class = new $current_section();

			do_action( 'woocommerce_update_options_payment_gateways123_' . $current_section_class->id );

			WC()->payment_gateways()->init();
		}
	}
}

endif;

return new WC_HelloWorld_Settings();
